﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Drawing;
using GreatB2C.Utility;

namespace GreatB2C.Web.Manage.SystemSet.Contents
{
    public partial class ModelManage : Inherit.ManagePage
    {
        Business.Model BLL = new Business.Model( );

        protected void Page_Load( object sender , EventArgs e )
        {
            if ( !IsPostBack )
            {
                LoadData( );
            }
        }

        private void LoadData( )
        {
            // 根据数据库对应的表，映射到相应的操作对象
            DataTable dtData = BLL.ListModelByType( 1 );
            DataView dvData = dtData.DefaultView;
            dvData.Sort = "ModelID asc";

            if ( dvData.Count > 0 )
            {
                AspNetPager1.RecordCount = dvData.Count;

                PagedDataSource pds = new PagedDataSource( );
                pds.DataSource = dvData;
                pds.AllowPaging = true;
                pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
                pds.PageSize = AspNetPager1.PageSize;
                dgData.DataSource = pds;
                dgData.DataBind( );

                dgData.Visible = true;
                litNone.Visible = false;
            }
            else
            {
                dgData.Visible = false;
                litNone.Text = "<div style='width:120px; margin:0 auto; margin-top:20px;'>没 有 任 何 数 据 ！</div>";
            }
        }

        protected void AspNetPager1_PageChanged( object src , EventArgs e )
        {
            LoadData( );
        }

        protected void dgData_ItemDataBound( object sender , DataGridItemEventArgs e )
        {
            if ( e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem )
            {
                //添加自定义属性，当鼠标移过来时设置该行的背景色,并保存原背景色
                e.Item.Attributes.Add( "onmouseover" , "this.className='tdbgmouseover';" );
                //添加自定义属性，当鼠标移走时还原该行的背景色
                e.Item.Attributes.Add( "onmouseout" , "this.className='tdbg';" );
                //添加双击事件
                e.Item.Attributes.Add( "ondblclick" , "RowDblclick('ModelModify.aspx?Action=Modify&ModelID=" + e.Item.Cells[ 1 ].Text.Trim( ) + "');" );

                if ( e.Item.Cells[ 2 ].Text.Trim( ) != "" )
                {
                    e.Item.Cells[ 2 ].Text = "<img src='../../../Resource/Images/ModelIcon/" + e.Item.Cells[ 2 ].Text.Trim( ) + "' border='0' />";
                }

                if ( e.Item.Cells[ 7 ].Text.Trim( ).ToLower( ) == "true" )
                {
                    LinkButton lbtnForbidden = ( LinkButton )e.Item.FindControl( "lbtnForbidden" );
                    lbtnForbidden.Text = "启用";
                }
            }
        }

        protected void dgData_ItemCommand( object source , DataGridCommandEventArgs e )
        {
            bool isSuccess = false;
            string name = e.Item.Cells[ 3 ].Text.Trim( );
            string id = e.CommandArgument.ToString( );
            if ( id.Trim( ) != "" )
            {
                switch ( e.CommandName )
                {
                    case "ListFields":
                        Response.Redirect( "FieldManage.aspx?ModelType=1&ModelID=" + id + "&ModelName=" + Server.UrlEncode( e.Item.Cells[ 3 ].Text.Trim( ) ) );
                        break;
                    case "Forbidden":
                        bool disabled = true;
                        LinkButton lbtnForbidden = ( LinkButton )e.Item.FindControl( "lbtnForbidden" );
                        if ( e.Item.Cells[ 7 ].Text.Trim( ).ToLower( ) == "true" )
                        {
                            disabled = false;
                        }
                        isSuccess = BLL.UpdateModelStatus( Convert.ToInt32( id ) , disabled );
                        if ( isSuccess )
                        {
                            LoadData( );
                        }
                        else
                        {
                            litScript.Text = "<script type='text/javascript'>ShowError('操作失败','内容模型信息" + lbtnForbidden.Text + "失败！');</script>";
                        }
                        break;
                    case "Delete":
                        GreatB2C.Model.Model model = new GreatB2C.Model.Model( );
                        model.ModelID = Convert.ToInt32( id );
                        isSuccess = BLL.Delete( model );
                        if ( isSuccess )
                        {
                            litScript.Text = "<script type='text/javascript'>ShowInfoAndRedirect('操作成功','内容模型信息（" + name + "）删除成功！','ModelManage.aspx');</script>";
                        }
                        else
                        {
                            litScript.Text = "<script type='text/javascript'>ShowError('操作失败','内容模型信息（" + name + "）删除失败！');</script>";
                        }
                        break;
                    case "SaveAsModel":
                        Response.Redirect( "ModelTemplate.aspx?Action=AddModelToFieldTemplate&ModelID=" + id );
                        break;
                }
            }
        }
    }
}
